﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SweptVolume.CApSimulationHandler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SweptVolume;

public class CApSimulationHandler : IApSamplingActivator
{
  private const bool DEFAULT_REFRESH_MODE = true;
  protected ITxObject m_simulatingObject;
  protected TxSimulationPlayer m_simulationPlayer;
  private bool m_refreshDisplay;
  private List<TxBreakpointEvent> m_activePauseEvents;
  private bool m_stopOnCollisionOriginalSetting;
  private bool m_ignoreSignalsOriginalSetting;
  private bool m_autoErrorOutput;
  private List<string> m_errors;
  private bool m_forceReset;

  public bool RefreshDisplay
  {
    get => this.m_refreshDisplay;
    set => this.m_refreshDisplay = value;
  }

  public double CurrentTime => this.m_simulationPlayer.CurrentTime;

  public ITxObject SimulatingObject => this.m_simulatingObject;

  public bool AutomaticErrorOutput
  {
    set => this.m_autoErrorOutput = value;
  }

  public List<string> Errors => this.m_errors;

  public bool ForceReset
  {
    set => this.m_forceReset = value;
  }

  public CApSimulationHandler(ITxOperation op)
    : this()
  {
    this.m_simulatingObject = (ITxObject) op;
  }

  public CApSimulationHandler(ITxRoboticProgram program)
    : this()
  {
    this.m_simulatingObject = (ITxObject) program;
  }

  public CApSimulationHandler()
  {
    this.m_refreshDisplay = true;
    this.m_activePauseEvents = new List<TxBreakpointEvent>();
    this.m_stopOnCollisionOriginalSetting = TxApplication.Options.Collision.StopOnCollision;
    this.m_ignoreSignalsOriginalSetting = TxSimulationPlayer.IgnoreSignals;
    this.m_simulationPlayer = new TxSimulationPlayer();
    this.m_autoErrorOutput = true;
    this.m_errors = new List<string>();
    this.m_forceReset = false;
  }

  public virtual void init()
  {
    if (this.m_forceReset)
    {
      this.m_simulationPlayer.AskUserForReset(false);
      this.m_simulationPlayer.DoOnlyUnscheduledReset(false);
    }
    this.m_simulationPlayer.IsAutomaticErrorOutputEnabled = this.m_autoErrorOutput;
    if (this.m_simulatingObject is ITxOperation)
    {
      ITxOperation simulatingObject = this.m_simulatingObject as ITxOperation;
      this.m_simulationPlayer.SetOperation(simulatingObject);
      this.triggerSimulationInitializedEvent();
      this.toggleOffActivePauseEvents(simulatingObject);
    }
    else if (this.m_simulatingObject is ITxRoboticProgram)
    {
      ITxRoboticProgram simulatingObject = this.m_simulatingObject as ITxRoboticProgram;
      this.m_simulationPlayer.SetProgram(simulatingObject);
      this.triggerSimulationInitializedEvent();
      TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxOperation));
      foreach (ITxOperation directChildElement in (Collection<ITxObject>) simulatingObject.GetDirectChildElements((ITxTypeFilter) txTypeFilter))
        this.toggleOffActivePauseEvents(directChildElement);
    }
    TxApplication.Options.Collision.StopOnCollision = false;
    TxSimulationPlayer.IgnoreSignals = true;
  }

  public virtual bool maySample() => true;

  public virtual void beforeSample()
  {
  }

  public virtual void afterSample()
  {
  }

  public virtual void cleanup()
  {
    this.reactivatePauseEvents();
    TxApplication.Options.Collision.StopOnCollision = this.m_stopOnCollisionOriginalSetting;
    TxSimulationPlayer.IgnoreSignals = this.m_ignoreSignalsOriginalSetting;
    this.m_simulationPlayer.AskUserForReset(true);
    this.m_simulationPlayer.IsAutomaticErrorOutputEnabled = true;
  }

  public void startSimulation()
  {
    this.playSimulation();
    if (this.m_autoErrorOutput)
      return;
    this.m_errors = this.m_simulationPlayer.GetErrorsAndTraces((TxSimulationErrorType) 256 /*0x0100*/);
  }

  public void stopSimulation()
  {
    if (this.m_simulationPlayer != null)
      this.m_simulationPlayer.Stop();
    this.cleanup();
  }

  public void rewind()
  {
    if (this.m_simulationPlayer == null)
      return;
    bool flag = false;
    if (!this.m_refreshDisplay)
      flag = TxApplicationEx.FreezeDisplay();
    try
    {
      this.m_simulationPlayer.Rewind();
    }
    finally
    {
      if (flag)
        TxApplicationEx.UnfreezeDisplay();
    }
  }

  private void playSimulation()
  {
    if (this.m_refreshDisplay)
      this.m_simulationPlayer.Play();
    else
      this.m_simulationPlayer.PlayWithoutRefresh();
  }

  private void toggleOffActivePauseEvents(ITxOperation op)
  {
    if (!(op is ITxSimulationEventParentOperation))
      return;
    foreach (ITxObject simulationEvent in (Collection<ITxObject>) (op as ITxSimulationEventParentOperation).SimulationEvents)
    {
      if (simulationEvent is TxBreakpointEvent)
      {
        TxBreakpointEvent txBreakpointEvent = simulationEvent as TxBreakpointEvent;
        if (txBreakpointEvent.State == 1)
        {
          txBreakpointEvent.State = (TxEventState) 0;
          this.m_activePauseEvents.Add(txBreakpointEvent);
        }
      }
    }
  }

  private void reactivatePauseEvents()
  {
    foreach (TxBreakpointEvent activePauseEvent in this.m_activePauseEvents)
      activePauseEvent.State = (TxEventState) 1;
  }

  public event samplingOccurenceReachedEventHandler samplingOccurenceReached;

  protected virtual void triggerSamplingOccurenceReachedEvent()
  {
    if (this.samplingOccurenceReached == null)
      return;
    this.samplingOccurenceReached((object) this, new EventArgs());
  }

  internal event CApSimulationHandler_SimulationInitializedEventHandler SimulationInitialized;

  protected virtual void triggerSimulationInitializedEvent()
  {
    if (this.SimulationInitialized == null)
      return;
    this.SimulationInitialized((object) this, new EventArgs());
  }
}
